package com.localjoycommunity.cpdj.project.system.controller;

import com.localjoycommunity.cpdj.common.interceptor.help.NeedLogin;
import com.localjoycommunity.cpdj.common.utils.poi.ExcelUtil;
import com.localjoycommunity.cpdj.framework.aspectj.lang.annotation.Log;
import com.localjoycommunity.cpdj.framework.aspectj.lang.enums.BusinessType;
import com.localjoycommunity.cpdj.framework.web.controller.BaseController;
import com.localjoycommunity.cpdj.framework.web.domain.AjaxResult;
import com.localjoycommunity.cpdj.framework.web.page.TableDataInfo;
import com.localjoycommunity.cpdj.project.system.domain.UserAnswer;
import com.localjoycommunity.cpdj.project.system.service.IUserAnswerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 用户答题记录Controller
 *
 * @author aqie
 * @date 2020-11-16
 */
@RestController
@RequestMapping("/system/answer")
public class UserAnswerController extends BaseController {
    @Autowired
    private IUserAnswerService userAnswerService;

    @NeedLogin
    @GetMapping("/app/list")
    public TableDataInfo appList(UserAnswer userAnswer, HttpServletRequest req) {
        Integer userId = getUserId(req);
        userAnswer.setUserId(Long.valueOf(userId));
        List<UserAnswer> list = userAnswerService.selectUserAnswerList(userAnswer);
        return getDataTable(list);
    }

    /**
     * 查询用户答题记录列表
     */
    @GetMapping("/list")
    public TableDataInfo list(UserAnswer userAnswer) {
        startPage();
        List<UserAnswer> list = userAnswerService.selectUserAnswerList(userAnswer);
        return getDataTable(list);
    }

    /**
     * 导出用户答题记录列表
     */
    @Log(title = "用户答题记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(UserAnswer userAnswer) {
        List<UserAnswer> list = userAnswerService.selectUserAnswerList(userAnswer);
        ExcelUtil<UserAnswer> util = new ExcelUtil<UserAnswer>(UserAnswer.class);
        return util.exportExcel(list, "answer");
    }


    /**
     * 获取用户答题记录详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(userAnswerService.selectUserAnswerById(id));
    }

    /**
     * 新增用户答题记录
     */
    @Log(title = "用户答题记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserAnswer userAnswer) {
        return toAjax(userAnswerService.insertUserAnswer(userAnswer));
    }

    /**
     * 修改用户答题记录
     */
    @Log(title = "用户答题记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserAnswer userAnswer) {
        return toAjax(userAnswerService.updateUserAnswer(userAnswer));
    }

    /**
     * 删除用户答题记录
     */
    @Log(title = "用户答题记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(userAnswerService.deleteUserAnswerByIds(ids));
    }
}
